//
//  ARBuriedPoint.m
//  PKX
//
//  Created by 刘高升 on 2019/8/8.
//  Copyright © 2019 YangTengJiao. All rights reserved.
//

#import "ARBuriedPoint.h"
#import <CoreText/CoreText.h>

@implementation ARBuriedPoint
+ (instancetype)sharedInstance {
    static ARBuriedPoint *_sharedSingleton = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        // 要使用self来调用
        _sharedSingleton = [[self alloc] init];
        [_sharedSingleton loadCustomFont];
    });
    return _sharedSingleton;
}

- (void)loadCustomFont {
    NSArray *customFont = @[@"Montserrat-Bold",@"Montserrat-Light"];
    for (NSString *fontFileName in customFont) {
        [self loadCustomFont:fontFileName];
    }
    
}
- (void)loadCustomFont:(NSString*)fontFileName {
    NSString *fontPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"ARResource.bundle/Fontfiles/%@",fontFileName] ofType:@"otf"];
    if (!fontPath) {
        NSBundle *bundile = [NSBundle bundleForClass:[self class]];
        NSString *componentPath = [NSString stringWithFormat:@"/%@.otf",fontFileName];
        fontPath =  [bundile.bundlePath stringByAppendingString:componentPath];
    }
    if (!fontPath) {
        NSLog(@"Failed to load font: %@", fontFileName);
        return;
    }
    NSData *inData = [NSData dataWithContentsOfFile:fontPath];
    CFErrorRef error;
    CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)inData);
    CGFontRef font = CGFontCreateWithDataProvider(provider);
    if (!CTFontManagerRegisterGraphicsFont(font, &error)) {
        CFStringRef errorDescription = CFErrorCopyDescription(error);
        //        NSLog(@"Failed to load font: %@", errorDescription);
        CFRelease(errorDescription);
    }
    CFRelease(font);
    CFRelease(provider);
}
@end
